
#include "PolyPlane.h"

#include "leak.h"


int ClassifyPoint (Plane& plane, const float point[3]) {

	float result = plane.n[0] * point[0] + plane.n[1] * point[1] + plane.n[2] * point[2] + plane.d;

	if(result > epsilon)
	{
		return FRONT;
	} else
		if(result < -epsilon)
		{
			return BACK;
		}

	return ONPLANE;
}


int ClassifyPolygon (Plane& plane, Poly& poly) {

	int num_front = 0, num_back = 0, num_on_plane = 0;

	int num_points = poly.points.size() / 3;

	for( auto point = poly.points.begin(); point != poly.points.end(); point += 3 )
	{
		float result = plane.n[0] * point[0] + plane.n[1] * point[1] + plane.n[2] * point[2] + plane.d;
		if(result > epsilon)
		{
			num_front++;
		} else
			if(result < -epsilon)
			{
				num_back++;
			} else
			{
				num_on_plane++;
			}
	}


	if(num_front == num_points)
	{
		return FRONT;
	}
	
	if(num_back == num_points)
	{
		return BACK;
	}

	if(num_on_plane == num_points)
	{
		return ONPLANE;
	}

	return SPANNING;

}

int ClassifyPolygon2 (Plane& plane, Poly& poly) {

	int num_front = 0, num_back = 0, num_on_plane = 0;

	int num_points = poly.points.size() / 3;
	//for(int i = 0; i < num_points; i++)
	for( auto point = poly.points.begin(); point != poly.points.end(); point += 3 )
	{
		float result = plane.n[0] * point[0] + plane.n[1] * point[1] + plane.n[2] * point[2] + plane.d;

		if(result > epsilon)
		{
			num_front++;
		} else
			if(result < -epsilon)
			{
				num_back++;
			} else
			{
				num_on_plane++;
			}
	}

	if(num_front == num_points)
	{
		return FRONT;
	}
	
	/******************/

	if(num_back == num_points)
	{
		return BACK;
	}

	if(num_on_plane == num_points)
	{
		return ONPLANE;
	}

	if(num_front + num_on_plane == num_points)
	{
		return TOUCH_FRONT;
	}

	if(num_back + num_on_plane == num_points)
	{
		return TOUCH_BACK;
	}

	return SPANNING;

}
